﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.IO;

using EnvironmentalMonitor.Support.Device;
using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.WebApplication.Abstract
{
    /// <summary>
    /// 抽象页面类
    /// </summary>
    public abstract class AbstractPage : System.Web.UI.Page
    {
        /// <summary>
        /// 字符串参数
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>值</returns>
        public string ParameterString(string name)
        {
            string result = null;

            if (this.Request.QueryString[name] != null)
            {
                result = this.Request.QueryString[name];
            }

            if (this.Request.Form[name] != null)
            {
                result = this.Request.Form[name];
            }

            return result;
        }

        /// <summary>
        /// 布尔型参数
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public bool ParameterBoolean(string name, bool defaultValue)
        {
            bool result = defaultValue;

            string value = this.ParameterString(name);
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    result = bool.Parse(value);
                }
                catch (Exception exception)
                {
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                }
                finally
                {
                }
            }

            return result;
        }

        /// <summary>
        /// 整型参数
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public int ParameterInt(string name, int defaultValue)
        {
            int result = defaultValue;

            string value = this.ParameterString(name);
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    result = int.Parse(value);
                }
                catch (Exception exception)
                {
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                }
                finally
                {
                }
            }

            return result;
        }

        /// <summary>
        /// 非负短整型参数
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public ushort ParameterUshort(string name, ushort defaultValue)
        {
            ushort result = defaultValue;

            string value = this.ParameterString(name);
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    result = ushort.Parse(value);
                }
                catch (Exception exception)
                {
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                }
                finally
                {
                }
            }

            return result;
        }

        /// <summary>
        /// 非负字节整型参数
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public byte ParameterByte(string name, byte defaultValue)
        {
            byte result = defaultValue;

            string value = this.ParameterString(name);
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    result = byte.Parse(value);
                }
                catch (Exception exception)
                {
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                }
                finally
                {
                }
            }

            return result;
        }

        /// <summary>
        /// 探头类型枚举参数
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>值</returns>
        public DetectorTypes ParameterDetectorTypes(string name, DetectorTypes defaultValue)
        {
            DetectorTypes result = defaultValue;

            string value = this.ParameterString(name);
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    result = (DetectorTypes)int.Parse(value);
                }
                catch (Exception exception)
                {
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                }
                finally
                {
                }
            }

            return result;
        }

        public void ParameterFile(string name, string url)
        {
            if ((this.Request.Files[name] != null) && (this.Request.Files[name].InputStream != null) && (this.Request.Files[name].InputStream.Length > 0))
            {
                string file = this.MapPath(url);
                FileInfo fileInfo = new FileInfo(file);
                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }
                FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
                this.Request.Files[name].InputStream.CopyTo(fileStream);
                fileStream.Close();
            }
        }

        /// <summary>
        /// 引用jQuery样式
        /// </summary>
        public string ReferencejQueryCss
        {
            get
            {
                string result = string.Empty;

                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}javascript/easyui/themes/default/easyui.css\" />", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);
                stringBuilder.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}javascript/easyui/themes/icon.css\" />", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);

                result = stringBuilder.ToString();

                return result;
            }
        }

        /// <summary>
        /// 引用jQuery脚本
        /// </summary>
        public string ReferencejQueryJavascript
        {
            get
            {
                string result = string.Empty;

                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.AppendFormat("<script type=\"text/javascript\" src=\"{0}javascript/easyui/jquery-1.8.0.min.js\"></script>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);
                stringBuilder.AppendFormat("<script type=\"text/javascript\" src=\"{0}javascript/easyui/jquery.easyui.min.js\"></script>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);
                stringBuilder.AppendFormat("<script type=\"text/javascript\" src=\"{0}javascript/easyui/locale/easyui-lang-zh_CN.js\"></script>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);
                stringBuilder.AppendFormat("<script type=\"text/javascript\" src=\"{0}javascript/custom/jquery.custom.js\"></script>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);

                result = stringBuilder.ToString();

                return result;
            }
        }

        /// <summary>
        /// 引用jQuery UI脚本
        /// </summary>
        public string ReferencejQueryUiJavascript
        {
            get
            {
                string result = string.Empty;

                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}javascript/ui/css/ui-lightness/jquery-ui-1.8.24.custom.css\" />", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);
                stringBuilder.AppendFormat("<script type=\"text/javascript\" src=\"{0}javascript/ui/js/jquery-ui-1.8.24.custom.min.js\"></script>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);

                result = stringBuilder.ToString();

                return result;
            }
        }

        /// <summary>
        /// 引用样式
        /// </summary>
        public string ReferenceCss
        {
            get
            {
                string result = string.Empty;

                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}javascript/custom/global.css\" />", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath);

                result = stringBuilder.ToString();

                return result;
            }
        }

        /// <summary>
        /// 引用脚本
        /// </summary>
        public string ReferenceJavascript
        {
            get
            {
                string result = string.Empty;

                StringBuilder stringBuilder = new StringBuilder();

                result = stringBuilder.ToString();

                return result;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.Response.Expires = 0;
            this.Response.Buffer = true;
            this.Response.ExpiresAbsolute = DateTime.Now.AddYears(-1);
            this.Response.AddHeader("pragma", "no-cache");
            this.Response.CacheControl = "no-cache";

            if (Hardware.FatalError)
            {
                this.Response.Redirect(string.Format("{0}FatalError.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath));
            }
        }
    }
}